// components/modal/modal.js
Component({
  // options: {
  //   multipleSlots: true // 在组件定义时的选项中启用多slot支持
  // },
  /**
   * 组件的属性列表
   */
  properties: {
    //是否显示modal
    showLoading: {
      type: Boolean,
      value: false
    },
    showConnecting: {
      type: Boolean,
      value: false,
      observer: '_showConnected'
    },
    showConnectedSuccess: {
      type: Boolean,
      value: false,
      observer: '_showConnected'
    },
    showConnectedFail: {
      type: Boolean,
      value: false,
      observer: '_showConnected'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    intervals: [],
    connectingtimeout: false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    clearIntervals(){
      this.data.intervals.forEach(element => {
        clearInterval(element)
       });
    },
    closeModal() {
      this.setData({ 
        showLoading: false,
      });
      this.clearIntervals();
      if(this.data.connectingtimeout || this.properties.showConnectedFail || this.properties.showConnectedSuccess){
        this.setData({ 
          connectingtimeout: false,
          showConnecting: false,
          showConnectedFail: false,
          showConnectedSuccess: false
        });
      }
      var myEventDetail = {} // detail对象，提供给事件监听函数
      var myEventOption = {} // 触发事件的选项
      this.triggerEvent('hideLoading', myEventDetail, myEventOption)
    },
    _showConnected() {
      this.clearIntervals();
      console.log(this.properties.showConnecting , this.properties.showConnectedFail, this.properties.showConnectedSuccess)
      this.setData({connectingText:  '.'})
      if(this.properties.showConnectedSuccess){
        this.closeModal();//连接成功关闭弹窗
      }else if(this.properties.showConnecting ) {
        const that = this;
        setTimeout(() => {
          that.setData({connectingtimeout: true});
        }, 5500);
        that.dot = ['.'];
        var interval = setInterval(() => {
          if(that.dot.length < 3){
            that.dot.push('.');
          } else {
            that.dot = ['.']
          }
          // console.log(that.dot)
          that.setData({connectingText:  that.dot.join('') })
        }, 100);
        this.data.intervals.push(interval);
      } 
    }
  },
  lifetimes: {
    attached: function() {
      wx.$eventEmitter.off('showLoading');
      wx.$eventEmitter.on('showLoading', (data) => {
        this.setData({showLoading: data});
      })
    },
    detached: function() {
      this.clearIntervals();
      
    },
  },
})